package com.zry.traced.presenter

import android.content.Intent
import android.view.View
import com.zry.traced.MainActivity
import com.zry.traced.R
import com.zry.traced.adapter.HomePageAdapter
import com.zry.traced.animation.RecyclerViewUtilKt
import com.zry.traced.been.BasicBean
import com.zry.traced.been.HttpResponse
import com.zry.traced.been.LoginInfoBean
import com.zry.traced.retrofit.ShowUserView
import com.zry.traced.ui.activity.*
import com.zry.traced.utils.ACacheUtil
import com.zry.traced.utils.HomePageList
import com.zry.traced.utils.NetWorkUtils
import com.zry.traced.utils.ToastUtil


class MainPresenter(private val activity: MainActivity) : ShowUserView {

    private var password: String? = null
    private var username: String? = null
    private var loginInfo: LoginInfoBean? = null
    private var basicList: MutableList<BasicBean>? = null
    private var homePageAdapter: HomePageAdapter? = null
    fun mainHandlerClick(view: View) {
        when (view.id) {
            R.id.morePageLinear -> {
                val intent = Intent(activity, MoreOperateActivity::class.java)
                activity.startActivity(intent)
            }
        }
    }


    fun initView() {
        password = ACacheUtil().getPassword()
        username = ACacheUtil().getUsername()
        if (password != null && password != "" && username != null && username != "") {

            if (NetWorkUtils().isNetworkAvailable(activity)) {
                HttpServer(this).loginApi(username!!, password!!, "2", activity)
            } else {
                val basicBeanList: MutableList<BasicBean> =
                        HomePageList().getHomeList(ACacheUtil().getLoginInfo().BasicCode)
                basicList!!.removeAll(basicList!!)
                basicList!!.addAll(basicBeanList)
                homePageAdapter?.notifyDataSetChanged()
            }

        } else {
            val intent = Intent(activity, LoginActivity::class.java)
            activity.startActivity(intent)
        }
    }


    override fun toMainActivity(what: Int, data: Any) {
        val httpResponse: HttpResponse<LoginInfoBean> = data as HttpResponse<LoginInfoBean>
        if (httpResponse.State == "1") {
            loginInfo = httpResponse.ResultData
            ACacheUtil().setLoginInfo(loginInfo!!)
            ACacheUtil().setUsername(username!!)
            ACacheUtil().setPassword(password!!)
            if (httpResponse.ResultData.BasicCode.size > 0) {
                val basicBeanList: MutableList<BasicBean> =
                        HomePageList().getHomeList(httpResponse.ResultData.BasicCode)
                basicList!!.removeAll(basicList!!)
                basicList!!.addAll(basicBeanList)
                homePageAdapter?.notifyDataSetChanged()
            } else {
                ToastUtil.toast(activity, "您没有使用权限，请联系管理员获取权限")
            }

        } else {
            ToastUtil.toast(activity, httpResponse.Message)
            val intent = Intent(activity, LoginActivity::class.java)
            activity.startActivity(intent)
        }
    }


    fun initAdapter() {
        basicList = mutableListOf()
        homePageAdapter = HomePageAdapter(R.layout.item_home_page, basicList)
        activity.recyclerViewUtilKt!!.setAdapter(homePageAdapter!!)
        homePageAdapter?.setOnItemClickListener { adapter, view, position ->
            val basicBean = basicList!![position]
            when (basicBean.BasicName) {
                "入库采集" -> {
                    val intent = Intent(activity, StoreOrderListActivity::class.java)
                    intent.putExtra("loginInfo", loginInfo)
                    intent.putExtra("title", basicBean.BasicName)
                    activity.startActivity(intent)
                }
                "出库采集" -> {
                    val intent = Intent(activity, StoreOrderListActivity::class.java)
                    intent.putExtra("loginInfo", loginInfo)
                    intent.putExtra("title", basicBean.BasicName)
                    activity.startActivity(intent)
                }
                "装箱操作" -> {
                    val intent = Intent(activity, PackingOperationActivity::class.java)
                    intent.putExtra("loginInfo", loginInfo)
                    intent.putExtra("title", basicBean.BasicName)
                    activity.startActivity(intent)
                }
                "补码拆码", "操作记录" -> {
                    val intent = Intent(activity, ComplementCodeActivity::class.java)
                    intent.putExtra("loginInfo", loginInfo)
                    intent.putExtra("title", basicBean.BasicName)
                    activity.startActivity(intent)
                }
                "本地数据" -> {
                    val intent = Intent(activity, ComplementCodeActivity::class.java)
                    intent.putExtra("title", basicBean.BasicName)
                    activity.startActivity(intent)
                }
            }
        }
    }
}